/*
 * SpriteAnimation.cpp
 *
 *  Created on: 25 jan 2010
 *      Author: hule
 */

#include "SpriteAnimation.h"
#include "Image.h"
#include <SDL/SDL.h>
#include <iostream>

SpriteAnimation::SpriteAnimation(int rows, int cols,int frametime, Image *pic) {
	if(pic==NULL){
		cout<<"ochill";
	}
	this->rows=rows;
	this->cols=cols;
	width=pic->getWidth()/cols;
	height=pic->getHeight()/rows;

	current=0;
	this->frametime=frametime;
	duration = this->frametime;
}

SpriteAnimation::~SpriteAnimation() {
	// TODO Auto-generated destructor stub
}
/*
SDL_Surface *SpriteAnimation::getFrame(SDL_Surface *screen){
	SDL_Surface *frame;
	SDL_Rect clip;
	clip.x = (current%cols)*width;
	clip.y = (current/cols)*height;
	clip.w= width;
	clip.h=	height;

	  // 'display' is the surface whose format you want to match
	  //  if this is really the display format, then use the surface returned from SDL_SetVideoMode

	const SDL_PixelFormat& fmt = *(screen->format);
	frame= SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,
	                  fmt.BitsPerPixel,
	                  fmt.Rmask,fmt.Gmask,fmt.Bmask,fmt.Amask );
	SDL_BlitSurface( pic, &clip, frame, NULL );


	return frame;
}
*/

SDL_Rect SpriteAnimation::getFrame(){
	SDL_Rect clip;
	clip.x = (current%cols)*width;
	clip.y = (current/cols)*height;
	clip.w= width;
	clip.h=	height;

	return clip;
}


void SpriteAnimation::update(int elapsedTime){
	duration-=elapsedTime;
	if(duration<0){
		current+=(-duration/frametime)+1;
		duration=frametime;
		if(current>=(cols*rows)){
			current=0;
		}
	}
}



bool SpriteAnimation::getLoop(){
	return loop;
}

int SpriteAnimation::getWidth(){
	return width;
}
int SpriteAnimation::getHeight(){
	return height;
}

void SpriteAnimation::setLoop(bool loop){
	this->loop = loop;
}






